<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/3/18
 * Time: 16:45
 */

namespace app\common\service;


class Running
{
    public $cache ;
    public $key;
    public function __construct($key)
    {
        $this->key = 'RUNNING_TEST7_'.$key.date("Y-m-d");//每天的key自动重置。
        $this->cache = \Yii::$app->cache;
    }

    public function setRunning($run){
        $run = !!$run;
        $key = $this->getRunningKey();
        $cache = \Yii::$app->cache;
        $cache->set($key , $run);
//        echo "\nsetRunning|{$key}|$run|\n";
    }

    public function runStart(){
        $this->setRunning(true);
    }

    public function runEnd(){
        $this->setRunning(false);
    }

    public function getRunningKey(){
        return $this->key;
    }

    public function isRunning(){
        $key = $this->getRunningKey();
        $cache = \Yii::$app->cache;
        return !!$cache->get($key);
    }
}